package com.commont;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

/**
 * http请求
 */
@Slf4j
public class HttpUntil {
    public static final String DEF_CHATSET = "UTF-8";
    public static final int DEF_CONN_TIMEOUT = 30000;
    public static final int DEF_READ_TIMEOUT = 30000;
    //车系查询AK
    public static final String AK="e7e0886711004320";

    public static String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";


    /**
     * 查询最近上映的电影
     */
    public static JsonObject getHotMovie() throws Exception{
        String url="https://api-m.mtime.cn/Showtime/LocationMovies.api?locationId=290";
        String string=net (url,null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }

    /**
     * 查询即将上映的电影
     */
    public static JsonObject getHotMovieAway() throws Exception{
        String url="https://api-m.mtime.cn/Movie/MovieComingNew.api?locationId=290";
        String string=net (url,null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }

    /**
     * 获取电影的详情
     * @param id
     * @return
     * @throws Exception
     */
    public static JsonObject getHotMovieinfo(Integer id ) throws Exception{
        String string=net ("https://ticket-api-m.mtime.cn/movie/detail.api?locationId=290&movieId="+id+"",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }


    /**
     * 获取电影评论
     * @throws Exception
     */
    public static JsonObject getHotMovieinfoComment(Integer movieId ) throws Exception{
        String string=net ("https://ticket-api-m.mtime.cn/movie/hotComment.api?movieId="+movieId+"",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }
    /**
     * 获取电影剧照
     * @throws Exception
     */
    public static JsonObject getHotMovieinfoImg(Integer movieId ) throws Exception{
        String string=net ("https://api-m.mtime.cn/Movie/ImageAll.api?movieId="+movieId+"",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }

    /**
     * 获取演员
     * @param
     * @return
     * @throws Exception
     */
    public static JsonObject getHotMovieinfoActor(Integer movieId ) throws Exception{
        String string=net ("https://api-m.mtime.cn/Movie/MovieCreditsWithTypes.api?movieId="+movieId+"",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }

    /**
     * 获取预告片
     * @param
     * @return
     * @throws Exception
     */
    public static JsonObject getHotMovieinfoVideo(Integer movieId,Integer pageIndex ) throws Exception{
        String string=net ("https://api-m.mtime.cn/Movie/Video.api?pageIndex="+pageIndex+"&movieId="+movieId+"",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }

    /**
     * 获取演员详情
     * @param
     * @return
     * @throws Exception
     */
    public static JsonObject getActor(Integer ActorId) throws Exception{
        String string=net ("https://ticket-api-m.mtime.cn/person/detail.api?personId="+ActorId+"&cityId=290",null,null);
        JsonObject jsonObject=new JsonParser ().parse ( string ).getAsJsonObject ();
        return jsonObject;
    }


    public static void main(String[] args) {
        JsonObject jsonArray=getWeather ("sdfds");
        log.info ( "【json】={}",jsonArray);
    }

    /**
     * 获取汽车的的品牌
     * @return
     */
    public static JsonObject getCarbrand(){
        JsonObject jsonObject=new JsonObject ();
        try {
            String result=net ( "http://api.jisuapi.com/car/brand?appkey="+AK+"",null,"GET" );
            jsonObject=new JsonParser ().parse ( result ).getAsJsonObject ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return jsonObject;
    }

    /**
     * 根据车型获取车
     * @return
     */
    public static JsonObject getCarByparentid(Integer parentid){
        JsonObject jsonObject=new JsonObject ();
        try {
            String result=net ( "http://api.jisuapi.com/car/car?appkey="+AK+"&parentid="+parentid+"",null,"GET" );
            jsonObject=new JsonParser ().parse ( result ).getAsJsonObject ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return jsonObject;
    }

    /**
     * 根据品牌获取车型
     * @return
     */
    public static JsonObject getCarBytype(Integer parentid){
        JsonObject jsonObject=new JsonObject ();
        try {
            String result=net ( "http://api.jisuapi.com/car/type?appkey="+AK+"&parentid="+parentid+"",null,"GET" );
            jsonObject=new JsonParser ().parse ( result ).getAsJsonObject ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return jsonObject;
    }

    /**
     * 根据品牌获取车型
     * @return
     */
    public static JsonObject getCarBydetail(Integer carid){
        JsonObject jsonObject=new JsonObject ();
        try {
            String result=net ( "http://api.jisuapi.com/car/detail?appkey="+AK+"&carid="+carid+"",null,"GET" );
            jsonObject=new JsonParser ().parse ( result ).getAsJsonObject ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return jsonObject;
    }

    /**
     * 查找天气
     * @return
     */
    public static JsonObject getWeather(String ip){
        JsonObject jsonObject=new JsonObject ();
        try {
            String result=net ( "https://api.seniverse.com/v3/weather/now.json?key=rmpgoluga4obwptm&location="+ip+"",null,"GET" );
            jsonObject=new JsonParser ().parse ( result ).getAsJsonObject ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
        return jsonObject;
    }


    /**
     * @param strUrl 请求地址
     * @param params 请求参数
     * @param method 请求方法
     * @return 网络请求字符串
     * @throws Exception
     */
    public static String net(String strUrl, Map params, String method) throws Exception {
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        String rs = null;
        try {
            StringBuffer sb = new StringBuffer();
            if (method == null || method.equals("GET")) {
                //strUrl = strUrl + "?" + urlencode(params);
            }
            URL url = new URL(strUrl);
            conn = (HttpURLConnection) url.openConnection();
            if (method == null || method.equals("GET")) {
                conn.setRequestMethod("GET");
            } else {
                conn.setRequestMethod("POST");
                conn.setDoOutput(true);
            }
            conn.setRequestProperty("User-agent", userAgent);
            conn.setUseCaches(false);
            conn.setConnectTimeout(DEF_CONN_TIMEOUT);
            conn.setReadTimeout(DEF_READ_TIMEOUT);
            conn.setInstanceFollowRedirects(false);
            conn.connect();
           /* if (params != null && method.equals("POST")) {
                try {
                    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
                    out.writeBytes(urlencode(params));
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }*/
            InputStream is = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader (is, DEF_CHATSET));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sb.append(strRead);
            }
            rs = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                reader.close();
            }
            if (conn != null) {
                conn.disconnect();
            }
        }
        return rs;
    }

    //将map型转为请求参数型
    public static String urlencode(Map<String, Object> data) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry i : data.entrySet()) {
            try {
                sb.append(i.getKey()).append("=").append( URLEncoder.encode(i.getValue() + "", "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return sb.toString();
    }


}
